package com.jt.controller;

import com.jt.pojo.*;
import com.jt.service.FileService;
import com.jt.vo.*;
import org.apache.commons.fileupload.disk.DiskFileItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.imageio.ImageIO;
import javax.imageio.stream.ImageInputStream;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.List;

@RestController
@CrossOrigin
@EnableFeignClients
public class AllController {
    @Bean
    @LoadBalanced
    public RestTemplate loadBalanceRestTemplate() {
        return new RestTemplate();
    }

    @Autowired
    private RestTemplate loadBalanceRestTemplate;

    @Autowired
    private FileService fileService;

    @RequestMapping("/aj-item/findItemList")    //查数据库里全部商品
    public List<ItemList> findItemList() {
        String url = String.format("http://aj-provider/item/findItemList");
        List<ItemList> list = loadBalanceRestTemplate.getForObject(url, List.class);
        return list;
    }

    @RequestMapping("/aj-item/recommendList")   //查数据库里Hot-ID=1的数据
    public List<ItemList> recommendList() {
        String url = String.format("http://aj-provider/item/recommendList");
        List<ItemList> recommendList = loadBalanceRestTemplate.getForObject(url, List.class);
        return recommendList;
    }

    @RequestMapping("/aj-item/HotList")         //查数据库内Hot_id=2的数据
    public List<ItemList> HotList() {
        String url = String.format("http://aj-provider/item/HotList");
        List<ItemList> HotList = loadBalanceRestTemplate.getForObject(url, List.class);
        return HotList;
    }

    @RequestMapping("/aj-item/saveItem")
    public SysResult addItem(@RequestBody ItemVO itemVO) {
        String url = String.format("http://aj-provider/item/saveItem");
        return loadBalanceRestTemplate.postForObject(url,itemVO,SysResult.class);
    }

    @RequestMapping("/aj-item/toQuery")
    public List<ItemList> toQuery(@RequestBody Query query) {
        String url = String.format("http://aj-provider/item/toQuery");
        return loadBalanceRestTemplate.postForObject(url,query,List.class);
    }

    @RequestMapping("/aj-itemCat/getLeft")
    public List<ItemCat> getLeft() {
        String url = String.format("http://aj-provider/itemCat/getLeft");
        return loadBalanceRestTemplate.getForObject(url, List.class);
    }

    @RequestMapping("/aj-user/reg")
    public String reg(@RequestBody User user) {
        String url = String.format("http://aj-provider/user/reg");
        return loadBalanceRestTemplate.postForObject(url,user,String.class);
    }

    @PostMapping("/aj-user/login")
    public User login(@RequestBody User user) {
        String url = String.format("http://aj-provider/user/login");
        User u = loadBalanceRestTemplate.postForObject(url,user,User.class);
        if (u == null) {
            return null;
        }
        return u;
    }

    @RequestMapping("/aj-user/list")
    public SysResult findUserListByPage(@RequestBody PageResult pageResult){
        String url = String.format("http://aj-provider/user/list");
        return loadBalanceRestTemplate.postForObject(url,pageResult,SysResult.class);
    }

    @RequestMapping( "/aj-user/status")
    public SysResult updateStatus(@RequestBody User user){
        String url = String.format("http://aj-provider/user/status");
        loadBalanceRestTemplate.put(url,user,User.class);
        return SysResult.success();
    }

    @RequestMapping("/aj-user/updateUserAdmin")
    public SysResult updateUser(@RequestBody User user){
        String url = String.format("http://aj-provider/user/updateUserAdmin");
        return loadBalanceRestTemplate.postForObject(url,user,SysResult.class);
    }

    @RequestMapping("/aj-user/id")
    public SysResult findUserById(@RequestBody User user){
        String url = String.format("http://aj-provider/user/id");
        return loadBalanceRestTemplate.postForObject(url,user,SysResult.class);
    }

    @RequestMapping( "/aj-user/dlid")
    public SysResult deleteUserById(@RequestBody User user){
        String url = String.format("http://aj-provider/user/dlid");
        return loadBalanceRestTemplate.postForObject(url,user,SysResult.class);
    }

    @RequestMapping("/aj-user/addUser")
    public SysResult addUser(@RequestBody User user){
        String url = String.format("http://aj-provider/user/addUser");
        return loadBalanceRestTemplate.postForObject(url,user,SysResult.class);
    }

    @RequestMapping("/aj-user/updateUser")
    public SysResult updateUser2(@RequestBody User user) {
        String url = String.format("http://aj-provider/user/updateUser");
        return loadBalanceRestTemplate.postForObject(url,user,SysResult.class);
    }

    @RequestMapping("/aj-user/showNumAndPrice")
    public NumAndPriceVO showNumAndPrice(@RequestBody User user){
        String url = String.format("http://aj-provider/user/showNumAndPrice");
        return loadBalanceRestTemplate.postForObject(url,user,NumAndPriceVO.class);
    }

    @RequestMapping("/aj-user/fingOne")
    public SysResult fingAll(@RequestBody User user){
        String url = String.format("http://aj-provider/user/fingOne");
        return loadBalanceRestTemplate.postForObject(url,user,SysResult.class);
    }


    @RequestMapping("/aj-itemSort/findItemSortList")
    public SysResult findItemSortList(@RequestBody ItemCat itemCat){
        String url = String.format("http://aj-provider/itemSort/findItemSortList");
        return loadBalanceRestTemplate.postForObject(url,itemCat,SysResult.class);
    }

    @RequestMapping("/aj-shoppingAdd")
    public String shoppingAdd(@RequestBody ShoppingVo shoppingVo){
        String url = String.format("http://aj-provider/shoppingAdd");
        return loadBalanceRestTemplate.postForObject(url,shoppingVo,String.class);
    }


    @RequestMapping("/aj-file/getUser")
    public User getUser(Integer id){
        String url = String.format("http://aj-provider/file/getUser");
        return loadBalanceRestTemplate.postForObject(url,id,User.class);
    }

    @RequestMapping("/aj-file/addUpload")
    public SysResult addUpload(@RequestPart("file") MultipartFile file) {
        return fileService.addUpload(file);
    }

    @RequestMapping("/aj-email")
    public String sendEmail(@RequestBody Email email) {
        String url = String.format("http://aj-provider/email");
        return loadBalanceRestTemplate.postForObject(url,email,String.class);
    }



    @PostMapping("/aj-addCart")
    public void addCart(@RequestBody Cart cart){
        String url = String.format("http://aj-provider/addCart");
        loadBalanceRestTemplate.postForObject(url,cart,String.class);
    }

    @RequestMapping("/aj-findCart")
    public List<Shopping> findCart(@RequestBody User user){
        String url = String.format("http://aj-provider/findCart");
        return loadBalanceRestTemplate.postForObject(url,user,List.class);
    }

    @RequestMapping("/aj-decreaseCart")
    public void decreaseCart(@RequestBody Shopping shopping){
        String url = String.format("http://aj-provider/decreaseCart");
        loadBalanceRestTemplate.postForObject(url,shopping,List.class);
    }

    @RequestMapping("/aj-addCart1")
    public void addCart(@RequestBody Shopping shopping){
        String url = String.format("http://aj-provider/addCart1");
        loadBalanceRestTemplate.postForObject(url,shopping,List.class);
    }


    @RequestMapping("/aj-deleteCart")
    public void deleteCart(@RequestBody Shopping shopping){
        System.out.println(shopping);
        String url = String.format("http://aj-provider/deleteCart");
        loadBalanceRestTemplate.postForObject(url,shopping,List.class);
    }

    @RequestMapping("/aj-deleteAllCart")
    public void deleteAllCart(@RequestBody User user){
        String url = String.format("http://aj-provider/deleteAllCart");
        loadBalanceRestTemplate.postForObject(url,user,List.class);
    }
}
